import sys
sys.path.append("..")
import numpy as np
from time import time


"""
def convolution_way2(img, kernelsize=2, kernel=None):
	imgshape = img.shape
	try:
		kernelsize = kernel.shape[0]
	except:
		kernel = np.random.random((kernelsize,kernelsize))
	out = np.zeros((imgshape[0]-(kernelsize-1),imgshape[1]-(kernelsize-1)))
	row = 0
	col = 0
	for r in kernel:
		for p in r:
			out += p*(img[row:row+imgshape[0]-(kernelsize-1),col:col+imgshape[1]-(kernelsize-1)])
			col += 1
		row += 1
		col = 0
	return out
"""


def convolution_way1(img, kernelsize=2, kernel=None):
	imgshape = img.shape
	try:
		kernelsize = kernel.shape[0]
	except:
		kernel = np.random.random((kernelsize,kernelsize))
	out = np.zeros((imgshape[0]-(kernelsize-1),imgshape[1]-(kernelsize-1)))
	row = 0
	col = 0
	for r in kernel:
		for p in r:
			out += (img*p)[row:row+imgshape[0]-kernelsize+1,col:col+imgshape[1]-kernelsize+1]
			col += 1
		row += 1
		col = 0
	return out



def convolution_way0(img, kernelsize=2, kernel=None):
	row = 0
	col = 0
	try:
		kernelsize = kernel.shape[0]
	except:
		kernel = np.random.random((kernelsize,kernelsize))
	out = np.zeros((img.shape[0]-(kernelsize-1),img.shape[1]-(kernelsize-1)))
	for i in range(out.shape[0]):
		for j in range(out.shape[1]):
			for x in range(kernelsize):
				for y in range(kernelsize):
					out[i,j] += img[i+x,j+y]*kernel[x,y]
	return out

if __name__ == '__main__':
	
	a = np.array([
		[1,2,3,4],
		[5,6,7,8],
		[9,1,2,3],
		[4,5,6,7]])
	k = np.array([
		[1,2],
		[3,4]])
	out = convolution_way0(a,kernel=k)
	print(out)

	"""
	a = np.random.random((100,100))
	loops = 10
	
	st = time()
	for i in range(loops):
		convolution_way0(convolution_way0(a,kernelsize=3),kernelsize=3)
	print(t1:=time()-st)
	
	st = time()
	for i in range(loops):
		convolution_way1(convolution_way1(a,kernelsize=3),kernelsize=3)
	print(t2:=time()-st)
	
	print(t1/t2)
	"""



















